package TXPathFinder;

import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.PickTool;
import com.sun.j3d.utils.picking.behaviors.PickMouseBehavior;
import java.awt.event.MouseEvent;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;

/**
*       \class	PickHighlightAvatar
*	\brief	Gère le highLight des avatars (la séléction des avatars dans le map3D)
**/
class PickHighlightAvatar extends PickMouseBehavior
{

    private Appearance savedAppearance = null;
    private Shape3D oldShape = null;
    private Integer ident;
    private Appearance highlightAppearance;

    /**
    *	\brief	Constructeur
    *   \param canvas Le canvas3D ou est stocker la viewer3D
    *   \param root La racine des objets 3D
    **/
    public PickHighlightAvatar(Canvas3D canvas, BranchGroup root)
    {
        super(canvas, root, new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0));
        this.setSchedulingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0));
        root.addChild(this);

        //definission des couleurs pour la surbrillance
        Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
        Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
        Color3f highlightColor = new Color3f(0.5f, 0.2f, 1.0f);
        highlightAppearance = new Appearance();
        highlightAppearance.setMaterial(new Material(highlightColor, black,
        highlightColor, white, 20.0f));
        pickCanvas.setMode(PickTool.BOUNDS);
    }

    /**
    *	\brief	Changement de couleur sur la forme sélectionnée
    *   \param xpos Position x de la sélection
    *   \param ypos Position y de la sélection
    **/
    public void updateScene(int xpos, int ypos)
    {
        if(this.mevent.getButton() == MouseEvent.BUTTON1)
        {
            PickResult pickResult = null;
            Shape3D shape = null;

            pickCanvas.setShapeLocation(xpos, ypos);

            pickResult = pickCanvas.pickClosest();
            if (pickResult != null)
            {
                shape = (Shape3D) pickResult.getNode(PickResult.SHAPE3D);
                ident = Integer.parseInt(shape.getName());
                DataModel.getInstance().setFocusTaken(true);
                DataModel.getInstance().setIsAvatarSelected(true);
                DataModel.getInstance().setAvatarCurrent(ident);
            }


            if(!DataModel.getInstance().isFocusTaken())
            {
                return;
            }
            if (oldShape != null)
            {
                oldShape.setAppearance(savedAppearance);
                ((Avatar)DataModel.getInstance().listAvatar.get(ident)).setIsSelected(false);
            }
            if (shape != null)
            {
                savedAppearance = shape.getAppearance();
                oldShape = shape;
                shape.setAppearance(highlightAppearance);
                ((Avatar)DataModel.getInstance().listAvatar.get(ident)).setIsSelected(true);
            }
        }
    }
}
